package ijij41.togetherTrain.recruit;

import ijij41.network.NetworkRequester;
import ijij41.togetherTrain.Constants;
import ijij41.togetherTrain.MainActivity;
import ijij41.togetherTrain.R;
import ijij41.togetherTrain.config.TrainConfig;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class RoomCreateActivity extends Activity implements Constants {

	private String m_date;
	private String m_trainNum;
	private String m_startTime;
	private String m_startStation;
	private String m_endTime;
	private String m_endStation;
	private String m_maxNumMember;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.room_create);

		initComponent();

	}

	private void initComponent() {

		// get parameters from before activity
		Intent intent = getIntent();

		int function = intent.getIntExtra("function", 0);

		// if (function == FROM_RESERVATION) {

		// Toast.makeText(RoomCreateActivity.this, "From reservation",
		// Toast.LENGTH_SHORT).show();

		m_date = intent.getStringExtra("date");
		m_trainNum = intent.getStringExtra("trainNum");
		m_startTime = intent.getStringExtra("startTime");
		m_startStation = intent.getStringExtra("startStation");
		m_endTime = intent.getStringExtra("endtTime");
		m_endStation = intent.getStringExtra("endStation");
		m_maxNumMember = intent.getStringExtra("numRecruitMember");
		// ///////////////////////

		// event handler
		((TextView) findViewById(R.id.tv_date)).setText(m_date);
		((TextView) findViewById(R.id.tv_trainNum)).setText(m_trainNum);
		((TextView) findViewById(R.id.tv_startTime)).setText(m_startTime);
		((TextView) findViewById(R.id.tv_startStation)).setText(m_startStation);
		((TextView) findViewById(R.id.tv_endTime)).setText(m_endTime);
		((TextView) findViewById(R.id.tv_endStation)).setText(m_endStation);
		((TextView) findViewById(R.id.tv_numMember)).setText(m_maxNumMember);

		// } else {
		//
		// Toast.makeText(RoomCreateActivity.this, "From new",
		// Toast.LENGTH_SHORT).show();
		// }

		// event handler
		findViewById(R.id.btn_back_img).setOnClickListener(mClickListener);
		findViewById(R.id.btn_create_room).setOnClickListener(mClickListener);

	}

	private Button.OnClickListener mClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btn_back_img:
				finish();
				break;

			// public boolean createNewRoom(String date, String num_train,
			// String start_time, String start_station, String dest_time,
			// String dest_station, String num_member, String master_id) {
			//
			// }

			case R.id.btn_create_room:
				if (NetworkRequester.getInstance().createNewRoom(m_date,
						m_trainNum, m_startTime, m_startStation, m_endTime,
						m_endStation, m_maxNumMember, TrainConfig.user_id)) {
					Toast.makeText(RoomCreateActivity.this, "방생성 성공",
							Toast.LENGTH_SHORT).show();

					// TODO 바로 홈 엑티비티로 가기, 혹은 바로 원하는 Activity로 점프
					// http://mydevromance.tistory.com/category
					// Intent intentHome = new Intent(this, MainActivity.class);
					Intent intentHome = new Intent(RoomCreateActivity.this,
							MainActivity.class);
					intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intentHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					startActivity(intentHome);
					finish();
				} else {
					Toast.makeText(RoomCreateActivity.this, "방생성 실패",
							Toast.LENGTH_SHORT).show();
				}
				break;
			}

		}
	};

}
